﻿using System;
using System.Windows;
using System.Windows.Media;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.ViewModels;
using Com.PhilChuang.Mobile.Apps.LocationSender.Utils;
using Com.PhilChuang.Pmvp.Ui.Views.Impl.Pages;
using Com.PhilChuang.Pmvp.Ui.Views.Impl.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;

namespace Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.Pages
{
	public abstract class AbstractLocationSenderAppPageBase<TViewModel> : PageBase<TViewModel>
		where TViewModel : AbstractLocationSenderAppViewModel
	{
		#region Instance variables
		private Pushpin g_Pushpin;
		#endregion

		#region To implement
		protected abstract Map MapControl { get; }
		#endregion

		/// <summary>
		/// Occurs when the ViewModel is attached to the Page, perform manual binding here.
		/// Call base impl first.
		/// </summary>
		protected override void OnViewModelAttached ()
		{
			base.OnViewModelAttached ();
			SetPushpin (ViewModel.CurrentPosition);
		}

		protected override void OnViewModelPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnViewModelPropertyChanged (sender, e);

			if (e.PropertyName == INPCHelper.GetPropertyName (() => ViewModel.CurrentPosition))
			{
				SetPushpin (ViewModel.CurrentPosition);
			}
		}

		private void SetPushpin (Model.Data.GeoCoordinate gc)
		{
			Dispatcher.BeginInvoke (
				() => {
					if (gc == null)
					{
						if (g_Pushpin != null)
						{
							MapControl.Children.Remove (g_Pushpin);
							g_Pushpin.MouseLeftButtonUp -= g_Pushpin_MouseLeftButtonUp;
							g_Pushpin = null;
						}
						return;
					}
					var addPushpin = false;
					var setMapCenter = false;
					var location = ViewModel.CurrentPosition.ToWp7GeoCoordinate ();
					if (g_Pushpin == null)
					{
						g_Pushpin = new Pushpin ();
						g_Pushpin.Background = Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
						g_Pushpin.Content = "Here I Am!";
						g_Pushpin.MouseLeftButtonUp += g_Pushpin_MouseLeftButtonUp;
						addPushpin = true;
						setMapCenter = true;
					}
					else if (g_Pushpin.Location != null)
					{
						var lastLocation = g_Pushpin.Location;
						var distance = location.GetDistanceTo (lastLocation);
						setMapCenter = distance > 50.0d;
					}
					g_Pushpin.Location = location;
					if (addPushpin)
					{
						MapControl.Children.Add (g_Pushpin);
					}
					if (setMapCenter)
					{
						MapControl.ZoomLevel = 15;
						MapControl.Center = g_Pushpin.Location;
					}

					// CODE FOR SCREENSHOT CAPTURE
					//SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
					//Orientation = PageOrientation.Landscape;
					//g_Pushpin.Content = "Location\nSender";
					//MapControl.ZoomLevel = 0;
					//((AerialMode) MapControl.Mode).ShouldDisplayLabels = false;
					//foreach (var c in ((System.Windows.Controls.Grid) MapControl.Parent).Children)
					//{
					//    if (c == MapControl) continue;
					//    c.Visibility = Visibility.Collapsed;
					//}
				});
		}

		private void g_Pushpin_MouseLeftButtonUp (object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			if (MapControl.ZoomLevel >= 20) // max
				MapControl.ZoomLevel = 10; // reset a bit
			else
				MapControl.ZoomLevel += 5;
			MapControl.Center = g_Pushpin.Location;
		}
	}
}
